package com.app.service.impl;

import com.app.dao.CollectDao;
import com.app.service.CollectServer;
import com.app.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.entity.Collect;
import com.common.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 王鑫
 * @create 2020 -11 -24 19:58
 */
@Service
public class CollectServerImpl extends ServiceImpl<CollectDao, Collect> implements CollectServer {
    @Autowired
    UserService userService;

    /**
     * 添加收藏
     */
    @Override
    public void addCollect(String userId, String pileId) {
        Collect collect = new Collect();
        String userId1 = collect.getUserId();
        User user = userService.getById(userId1);
        String pileId1 = collect.getPileId();
        baseMapper.insert(collect);
    }

    /**
     * 通过id删除收藏
     *
     * @param id
     */
    @Transactional
    @Override
    public void deleteCollectById(String id) {
        baseMapper.deleteById(id);
    }

    /**
     * 通过id查询收藏
     *
     * @param id
     * @return
     */
    @Override
    public Collect selectCollectById(String id) {
        Collect collect = baseMapper.selectById(id);
        String userId = collect.getUserId();
        String pileId = collect.getPileId();

        return collect;
    }


    /**
     * 查询所有收藏
     *
     * @return
     */
    @Override
    public List<Collect> selectAllCollect() {
        List<Collect> collects = baseMapper.selectList(null);
        return collects;
    }

    /**
     * 分页查询所有收藏
     *
     * @param page
     */
    @Override
    public void selectCollectByPage(Page<Collect> page) {
        baseMapper.selectPage(page, null);
    }

    /**
     * 多条件分页查询收藏
     *
     * @param page
     * @param collect
     */
    @Override
    public void selectCollectByPageAndCondition(Page<Collect> page, Collect collect) {
        String userId = collect.getUserId();
        String pileId = collect.getPileId();
        Date createTime = collect.getCollectCreateTime();
        Date modifyTime = collect.getCollectModifyTime();

        //构造查询条件
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(userId)) {
            queryWrapper.like("user_id", userId);
        }
        if (!StringUtils.isEmpty(pileId)) {
            queryWrapper.like("pile_id", pileId);
        }
        if (createTime != null) {
            queryWrapper.ge("collect_create_time", createTime);
        }
        if (modifyTime != null) {
            queryWrapper.le("collect_modify_time", modifyTime);
        }
        //启动分页
        baseMapper.selectPage(page, queryWrapper);
    }


    /**
     * 通过用户id查询收藏记录
     */
    @Override
    public List<Collect> selectCollectByUserId(String userId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Collect> collects = baseMapper.selectList(queryWrapper);
        return collects;
    }

    /**
     * 通过用户账号(电话)查询收藏记录
     */
    @Override
    public List<Collect> selectCollectByUserPhone(String userPhopne) {
        String userId = userService.getUserId(userPhopne);
        List<Collect> collectList = selectCollectByUserId(userId);
        return collectList;
    }

}
